import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet(urlPatterns = {"/signup"})
public class signup extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
	String URL = getServletContext().getInitParameter("db.url");
	String NAME = getServletContext().getInitParameter("db.user");
	String PASSWORD = getServletContext().getInitParameter("db.password");

	System.out.println(req.getParameter("name"));
	UserBean userBean = new UserBean( URL, NAME, PASSWORD, req.getParameter("username"), req.getParameter("passwd"), req.getParameter("name").substring(0, 1), req.getParameter("name").substring(1, req.getParameter("name").length()), req.getParameter("email"))  ;
	(new DatabaseConnect(getServletContext())).updatesql(userBean);


	Map<String, String> message = new LinkedHashMap<String, String>();
	message.put("用户名", userBean.getUsername());
	message.put("姓", userBean.getFamilyname());
	message.put("名", userBean.getName());
	message.put("邮箱", userBean.getEmail());
	HttpSession session = req.getSession(true);
	if(req.getParameter("net_auto_login") != null)
	    session.setMaxInactiveInterval(7 * 24 * 60 * 60);
	session.setAttribute("Login", message);
	session.setAttribute("username", userBean.getUsername());
	    DatabaseConnect db= new DatabaseConnect(getServletContext());
	    if(db.getimage(userBean.getUsername()) != null)
		session.setAttribute("portrait", db.getimage(userBean.getUsername()));
	try {
	    System.out.println("about to dispatch");
	    req.getRequestDispatcher("profile.jsp").forward(req, res);
	} catch (ServletException e) {
	    e.printStackTrace();
	}

    }
}
